<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
   // $fields=array('id','name');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_sns_post',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['bid']=pdo_getcolumn('jy_sns_board',array('id'=>$v['bid']),'title');
		$name=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');
		$v['name']=$name?$name:'匿名';
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'话题管理'),
			array('op'=>'edit','title'=>'添加话题'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'bid','title'=>'所属版块','type'=>'string'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'name','title'=>'昵称','type'=>'string'),

		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'标题','type'=>'string'),
		)
	);
	$return['enabled']=array('0'=>'禁用','1'=>'启用');
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_sns_post',array('id'=>$id));
		$item['images']=json_decode($item['images'],true);
	}else{
		$item=array();
	}
    $return['item']=$item;
	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'话题设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'话题管理'),
			array('op'=>'edit','title'=>'添加话题')
		),
		'edit'=>array(
			array('name'=>'bid','title'=>'所属板块','type'=>'select','options'=>'bid'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('type'=>'choose','title'=>'发帖管理员','name'=>'mid','action'=>'get_member','choose_title'=>'选择管理员'),
			array('name'=>'images','title'=>'上传图片','type'=>'images'),
			array('name'=>'content','title'=>'话题内容','type'=>'richtextold'),
			array('name'=>'isboardbest','title'=>'版块精华','type'=>'switch','options'=>'is'),
			array('name'=>'isbest','title'=>'全站精华','type'=>'switch','options'=>'is'),
			array('name'=>'isboardtop','title'=>'版块置顶','type'=>'switch','options'=>'is'),
			array('name'=>'istop','title'=>'全站置顶','type'=>'switch','options'=>'is'),
			
			)
			);
	$return['is']=array('0'=>'否','1'=>'是');
	$return['bid']=pdo_getall('jy_sns_board',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写标题');
	}
	if(!$postdata['content']){
		exi('请填写内容');
	}
	$postdata['images']=ijson_encode($postdata['images']);
	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_sns_post',$postdata,array('id'=>$id));
				exi('设置成功！','','list');
			}else{
			    $postdata['uniacid']=$_SESSION['uniacid'];
				$postdata['createtime']=time();
				pdo_insert('jy_sns_post',$postdata);
				exi('添加成功！','','list');
			}
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_sns_post',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_sns_post',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>